//
//  DeteManager.m
//  KEREN
//
//  Created by MW on 2023/4/6.
//

#import "DeteManager.h"

@implementation DeteManager
+ (instancetype)shareInstance {
    static DeteManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (NSInteger)getCurrentDay{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    return (long)[datenow timeIntervalSince1970];
}
- (NSInteger)getLastDay:(int)lastcount{
    NSDate *date = [NSDate date];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*lastcount sinceDate:date];//前一天
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = lastDay;//现在时间,你可以输出来看下是什么格式
    return (long)[datenow timeIntervalSince1970];
}

- (NSInteger)getDay:(NSString *)deteStr{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd";
    NSDate *date = [format dateFromString:deteStr];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [format setTimeZone:timeZone];
    return (long)[date timeIntervalSince1970];
}

-(NSInteger)monthDayWithYear:(NSInteger) year month:(NSInteger) month{
    // 字符串转日期
    NSString *dateStr = [NSString stringWithFormat:@"%d-%ld-01 00:00:00",year,month];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [format dateFromString:dateStr];
    // 当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return range.length;
}

-(NSInteger)monthDayWithdeteStr:(NSString *)deteStr{
    NSString *dateStr = [NSString stringWithFormat:@"%@ 00:00:00",deteStr];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [format dateFromString:dateStr];
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [format setTimeZone:timeZone];
    return (long)[date timeIntervalSince1970];
}

-(NSInteger)monthDayWithendDeteStr:(NSString *)deteStr{
    NSString *dateStr = [NSString stringWithFormat:@"%@ 23:59:59",deteStr];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [format dateFromString:dateStr];
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [format setTimeZone:timeZone];
    return (long)[date timeIntervalSince1970];
}

-(NSString *)timeFormatted:(NSInteger)totalSeconds{
    NSInteger time = totalSeconds;
    NSNumber *timer = [NSNumber numberWithInteger:time];
    NSTimeInterval interval = [timer doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    //设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH: mm"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}
-(NSString *)timeFormathhmm:(NSInteger)totalSeconds{
    NSInteger time = totalSeconds;
    NSNumber *timer = [NSNumber numberWithInteger:time];
    NSTimeInterval interval = [timer doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    //设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"HH: mm"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}
-(NSString *)timeFormatYYYYMMdd:(NSInteger)totalSeconds{
    NSInteger time = totalSeconds;
    NSNumber *timer = [NSNumber numberWithInteger:time];
    NSTimeInterval interval = [timer doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    //设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}


-(NSString *)timeFormatMMddhhmm:(NSInteger)totalSeconds{
    NSInteger time = totalSeconds;
    NSNumber *timer = [NSNumber numberWithInteger:time];
    NSTimeInterval interval = [timer doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    //设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH: mm"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}

-(NSString *)timeFormatHHmmss:(NSInteger)totalSeconds{
    NSInteger time = totalSeconds;
    NSNumber *timer = [NSNumber numberWithInteger:time];
    NSTimeInterval interval = [timer doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    //设置日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}


- (NSString *)getTodayStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *str = [formatter stringFromDate:[NSDate date]];
    return str;

}



- (NSString *)lastDay:(NSInteger)index AndType:(NSInteger)type{
    NSDate *date = [NSDate date];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*index sinceDate:date];//前一天
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if(type == 0){
        [formatter setDateFormat:@"yyyy-MM-dd"];
    }else{
        [formatter setDateFormat:@"MM.dd"];
    }
    NSString *the_date_str = [formatter stringFromDate:lastDay];
    return the_date_str;
}


@end
